package com.syl.web.admin;

import com.github.pagehelper.PageInfo;
import com.syl.model.ResponseData.ArticleResponseData;
import com.syl.model.ResponseData.StaticticsBo;
import com.syl.model.domain.Article;
import com.syl.model.domain.Comment;
import com.syl.service.IArticleService;
import com.syl.service.ISiteService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin")
public class AdminController {

  private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

  @Autowired
  private ISiteService siteServiceImpl;
  @Autowired
  private IArticleService articleServiceImpl;

  // 管理中心起始页
  @GetMapping(value = {"", "/index"})
  public String index(HttpServletRequest request) {
    // 获取最新的5篇博客、评论以及统计数据
    List<Article> articles = siteServiceImpl.recentArticles(5);
    List<Comment> comments = siteServiceImpl.recentComments(5);
    StaticticsBo staticticsBo = siteServiceImpl.getStatistics();
    // 向Request域中存储数据
    request.setAttribute("comments", comments);
    request.setAttribute("articles", articles);
    request.setAttribute("statistics", staticticsBo);
    return "back/index";
  }

  // 向博文发表页面跳转
  @GetMapping(value = "/article/toEditPage")
  public String newArticle() {
    return "back/article_edit";
  }

  // 发表博文
  @PostMapping(value = "/article/publish")
  @ResponseBody
  public ArticleResponseData publishArticle(Article article) {
    if (StringUtils.isBlank(article.getCategories())) {
      article.setCategories("默认分类");
    }
    try {
      articleServiceImpl.publish(article);
      logger.info("博文发布成功");
      return ArticleResponseData.ok();
    } catch (Exception e) {
      logger.error("博文发布失败，错误信息: " + e.getMessage());
      return ArticleResponseData.fail();
    }
  }

  // 跳转到后台博文列表页面
  @GetMapping(value = "/article")
  public String index(@RequestParam(value = "page", defaultValue = "1") int page,
      @RequestParam(value = "count", defaultValue = "10") int count,
      HttpServletRequest request) {
    PageInfo<Article> pageInfo = articleServiceImpl.selectArticleWithPage(page, count);
    request.setAttribute("articles", pageInfo);
    return "back/article_list";
  }

  // 向博文修改页面跳转
  @GetMapping(value = "/article/{id}")
  public String editArticle(@PathVariable("id") String id, HttpServletRequest request) {
    Article article = articleServiceImpl.selectArticleWithId(Integer.parseInt(id));
    request.setAttribute("contents", article);
    request.setAttribute("categories", article.getCategories());
    return "back/article_edit";
  }

  // 博文修改处理
  @PostMapping(value = "/article/modify")
  @ResponseBody
  public ArticleResponseData modifyArticle(Article article) {
    try {
      articleServiceImpl.updateArticleWithId(article);
      logger.info("博文更新成功");
      return ArticleResponseData.ok();
    } catch (Exception e) {
      logger.error("博文更新失败，错误信息: " + e.getMessage());
      return ArticleResponseData.fail();
    }
  }

  // 博文删除
  @PostMapping(value = "/article/delete")
  @ResponseBody
  public ArticleResponseData delete(@RequestParam int id) {
    try {
      articleServiceImpl.deleteArticleWithId(id);
      logger.info("博文删除成功");
      return ArticleResponseData.ok();
    } catch (Exception e) {
      logger.error("博文删除失败，错误信息: " + e.getMessage());
      return ArticleResponseData.fail();
    }
  }


}

